<?php


namespace Database\Seeders;


use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class InitHealthDataSeeder extends Seeder
{
    public function run()
    {
        DB::beginTransaction();
        try {
            $this->initAcuData();
            $this->initBodyData();
            $this->initMeridianData();

            $this->initDiseaseClassData();
            $this->initDiseaseData();
            $this->initBreathe();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            log_exception('init health_db', $e);
            dd($e->getMessage());
        }
    }

    protected function initAcuData()
    {
        $data = json_decode(file_get_contents(storage_path('health_db_data/xunai_acupoint.json')), true);

        $acuArr = [];
        $bodyPartArr = [];
        $acuMeridianArr = [];
        $acuBodyArr = [];
        $acuBodyPartArr = [];

        foreach ($data as $item) {

            $acuArr[] = [
                'id'                => $item['acupointId'],
                'name'              => $item['acupointName'],
                'description'       => $item['description'],
                'pin_yin'           => $item['acupointPinyin'],
                'point_description' => $item['pointDescription'],
                'img'               => $item['acupointPic'],
                'point_img'         => $item['imageUrl'],
                'video_file'        => $item['fileUrl'],
                'created_at'        => date('Y-m-d H:i:s'),
                'updated_at'        => date('Y-m-d H:i:s'),
            ];

            if (!isset($bodyPartArr[$item['bodyPartId']])) {
                $bodyPartArr[$item['bodyPartId']] = [
                    'id'         => $item['bodyPartId'],
                    'name'       => $item['bodyPartName'],
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }

            $acuMeridianArr[] = [
                'acu_id'      => $item['acupointId'],
                'meridian_id' => $item['meridianId']
            ];

            $acuBodyArr[] = [
                'acu_id'  => $item['acupointId'],
                'body_id' => $item['bodyId']
            ];

            $acuBodyPartArr[] = [
                'acu_id'       => $item['acupointId'],
                'body_part_id' => $item['bodyPartId']
            ];

        }

        DB::table('health_acupoint')->insert($acuArr);
        DB::table('health_body_part')->insert(array_values($bodyPartArr));
        DB::table('health_acu_meridian')->insert($acuMeridianArr);
        DB::table('health_acu_body')->insert($acuBodyArr);
        DB::table('health_acu_body_part')->insert($acuBodyPartArr);
    }

    protected function initBodyData()
    {
        $data = json_decode(file_get_contents(storage_path('health_db_data/xunai_body.json')), true);

        $arr = [];

        foreach ($data as $item) {
            $arr[] = [
                'id'         => $item['bodyId'],
                'name'       => $item['bodyName'],
                'img'        => $item['bodyImage'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        DB::table('health_body')->insert($arr);
    }

    protected function initMeridianData()
    {
        $data = json_decode(file_get_contents(storage_path('health_db_data/xunai_meridian.json')), true);

        $arr = [];

        foreach ($data as $item) {
            $arr[] = [
                'id'         => $item['meridianId'],
                'name'       => $item['meridianName'],
                'abbr_name'  => $item['meridianAbbrName'],
                'img'        => $item['imagePic'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        DB::table('health_meridian')->insert($arr);
    }

    protected function initDiseaseClassData()
    {
        $data = json_decode(file_get_contents(storage_path('health_db_data/xunai_fenlei.json')), true);

        $classArr = [];

        foreach ($data as $item) {
            $classArr[] = [
                'id'         => $item['healthTypeId'],
                'name'       => $item['healthTypeName'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        DB::table('health_disease_class')->insert($classArr);
    }

    protected function initDiseaseData()
    {
        $diseaseData = json_decode(file_get_contents(storage_path('health_db_data/xunai_plan.json')), true);
        $diseaseSchemeData = json_decode(file_get_contents(storage_path('health_db_data/xunai_plan_acupoint.json')), true);

        $dArr = [];
        $dsArr = [];

        foreach ($diseaseData as $item) {
            $dArr[] = [
                'id'                  => $item['id'],
                'class_id'            => $item['healthTypeId'],
                'name'                => $item['planName'],
                'img'                 => $item['planPic'],
                'introduce'           => $item['symptomsIntroduction'] ?: '',
                'treat_details'       => $item['treatDetails'] ?: '',
                'clinical_experience' => $item['clinicalExperience'] ?: '',
                'case'                => $item['relatedCase'] ?: '',
                'created_at'          => date('Y-m-d H:i:s'),
                'updated_at'          => date('Y-m-d H:i:s'),
            ];
        }

        foreach ($diseaseSchemeData as $item) {
            $dsArr[] = [
                'disease_id'  => $item['xunai_plan_id'],
                'acu_id'      => $item['acupointId'],
                'description' => $item['description'],
                'day'         => $item['day'],
                'time'        => $item['acupointTime'],
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ];
        }

        DB::table('health_disease')->insert($dArr);
        DB::table('health_disease_scheme')->insert($dsArr);
    }

    protected function initBreathe()
    {
        $data = json_decode(file_get_contents(storage_path('health_db_data/xunai_breath.json')), true);

        $breathArr = [];

        foreach ($data as $item) {
            $breathArr[] = [
                'name'        => $item['breathName'],
                'introduce'   => $item['breathEffect'],
                'img'         => $item['breathPic'],
                'circle_num'  => $item['circleNum'],
                'inhale_time' => $item['inhaleTime'],
                'inhale_tip'  => $item['inhaleHint'],
                'exhale_time' => $item['exhaleTime'],
                'exhale_tip'  => $item['exhaleHint'],
                'stay_time'   => $item['maxStayTime'],
                'status'      => 1,
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s'),
            ];
        }

        DB::table('health_breathe')->insert($breathArr);
    }
}
